using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class Pages_Employee_ManageFaultDeliveries_EditFaultDelivery : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["id"] != null)
                FillFields();
            else
                Response.Redirect("ViewFaultDeliveries.aspx");
        }
    }

    private void FillFields()
    {
        try
        {
            int id = Int32.Parse(Request.QueryString["id"].ToString());
            List<FaultDelivery> list = DALFaultDelivery.GetFaultDeliveriesByID(id);
            if (list.Count < 1)
                Response.Redirect("~/Errors/ConnectionError.aspx");

            // fill the fields with information in FaultDelivery object
            FaultDelivery faultDelivery = list[0];
            txtDeliverableID.Text = faultDelivery.DeliverableID.ToString();
            cldFaultDate.SelectedDate = DateTime.ParseExact(faultDelivery.FaultDate, "dd/MM/yyyy", null);
            txtReason.Text = faultDelivery.Reason;
            txtRefundAmount.Text = faultDelivery.RefundAmount.ToString();
            cboIsRepaid.Checked = faultDelivery.IsRepaid;
        }
        catch (FormatException ex)
        {
            Response.Redirect("~/Errors/GeneralError.aspx");
        }
        catch (SqlException ex)
        {
            Response.Redirect("~/Errors/ConnectionError.aspx");
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;

        int id = Int32.Parse(txtDeliverableID.Text);
        string faultDate = cldFaultDate.SelectedDate.ToString("MM/dd/yyyy");
        string reason = txtReason.Text;
        decimal refundAmount = decimal.Parse(txtRefundAmount.Text.Trim());
        bool isRepaid = cboIsRepaid.Checked;

        int affected;
        try
        {
            affected = DALFaultDelivery.UpdateFaultDelivery(id, faultDate, reason, refundAmount, isRepaid);
            if (affected > 0)
                lblMessage.Text = "Edited successfully";
            else
                lblMessage.Text = "Error editing fault delivery";
        }
        catch
        {
            lblMessage.Text = "Error editing fault delivery.";
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        FillFields();
        lblMessage.Text = "";
    }

    // Checks whether the selected date is valid, it must be in the past or today
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (DateTime.Compare(cldFaultDate.SelectedDate, DateTime.Today) > 0)
        {
            args.IsValid = false;
        }
    }

    // Checks whether the Refund field is a positive decimal number
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            decimal n = decimal.Parse(args.Value);
            args.IsValid = (n >= 0);
        }
        catch (FormatException ex)
        {
            args.IsValid = false;
        }
    }
}
